﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyOA.Model;
using System.Data.SqlClient;

namespace MyOA.DAL
{
    public class LoginService
    {
        DBHelper db = new DBHelper();

        /// <summary>
        /// 查看用户是否登陆
        /// </summary>
        /// <param name="loginUser">输入的用户信息</param>
        /// <returns>返回是否是存在的用户</returns>
        public bool isLoginSuccessed(UserInfo loginUser)
        {

            SqlParameter[] sqlParams = new SqlParameter[]
           {
               new SqlParameter("@loginId",loginUser.LoginId),
               new SqlParameter("@loginPwd",loginUser.Password)
           };

            if ((int)db.getScalar("sp_isLoginSuccessed", sqlParams) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 增加登陆日志的信息
        /// </summary>
        /// <param name="loginLog">要增加的登陆日志信息</param>
        public void insertLoginLog(LoginLog loginLog)
        {
            SqlParameter[] value = new SqlParameter[]
           {
               new SqlParameter("@loginTime",loginLog.LoginTime),
               new SqlParameter("@ifSuccess",int.Parse(loginLog.IfSuccess)),
               new SqlParameter("@loginUserIp",loginLog.LoginUserIp),
               new SqlParameter("@userId",loginLog.User.UserId)
               
           };
            db.getCount("sp_insertLoginLog", value);
        }

        /// <summary>
        /// 根据登陆名找登陆ID
        /// </summary>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public int getUserIdByLoginId(string loginId)
        {
            return (int)db.getScalar("sp_getUserIdByLoginId", new SqlParameter("@loginId", loginId));
        }
    }
}
